<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class Adcontroller extends Controller
{
    public function getAdd()
    {
    
    	$compname=DB::table('enterprises')->where('ad_status',1)->get();
    	$position=DB::table('ad_position')->get();
        return  view('admin.ad.add',['compname'=>$compname,'position'=>$position]);   	
    }
    //添加广告位置页面
    public function getAddpos()
    {
    
        return  view('admin.ad.addpos');     
    }//添加广告位置页面

        //添加广告位置操作
     public function postInsertpos(Request $request)
    {
        $data=$request->except('_token');
        
        $res=DB::table('ad_position')->insert($data);
        if($res)
        {
            return redirect('/admin/ad/index')->with(['info'=>'添加成功']);
        }else
        {
            return back()->with(['info'=>'添加失败']);
        }
    }//添加广告位置操作




//添加
    public function postInsert(Request $request)
    {
    	$data=$request->except('_token');
    
    	if($request->hasFile('img')) 
    	{
    	
    		if($request->file('img')->isValid())

    		{
    			
    			$ext=$request->file('img')->getClientOriginalExtension();
    			$filename=time().mt_rand(100000,999999).'.'.$ext;
    			$request->file('img')->move('./ads',$filename);
    			$data['img']=$filename;
    		}
    	}
    	 
    	 
    	 
    	$data['status']=1;
    	$res=DB::table('ad')->insert($data);




    	if($res)
        {
            return redirect('/admin/ad/index')->with(['info'=>'添加成功']);
        }else
        {
            return back()->with(['info'=>'添加失败']);
        }
    }
//添加
//广告主页
    public function getIndex(Request $request)
    {
    	
    	$data=DB::table('ad')
    				->select('ad.*','enterprises.compname','ad_position.position as pos')
    				->leftJoin('enterprises','ad.name','=','enterprises.id')
    				->leftJoin('ad_position','ad.position','=','ad_position.id')
					->where("enterprises.compname","like","%".$request->input('search')."%" )
					->orderBy('ad.position')
					->orderBy('ad.id')
					->paginate($request->input('pages',10));
       	

    	return view('admin.ad.index',['data'=>$data,'request'=>$request->all()]);

    }
//广告主页
//编辑
   public function getEdit($id)
   {
     	$compname=DB::table('enterprises')->where('ad_status',1)->get();
    	$position=DB::table('ad_position')->get();
   	    $data=DB::table('ad')->where('id',$id)->first();
   	  if($data)
		{
			return view('admin.ad.edit',['data'=>$data,'compname'=>$compname,'position'=>$position]);
			dd($data);
		}else
		{

			return back()->with(['info'=>'编辑错误']);
		}
   	  
   }
//编辑
//编辑修改
	public function postUpdate(Request $request)
	{
		$data=$request->except('_token');
		$id=$request->id;
		$old=DB::table('ad')->where('id',$id)->first();
		$oldimg='./ads/'.$old->img;
		
	
		if($request->hasFile('img'))
		{
			if($request->file('img')->isValid())
			{

				if (file_exists($oldimg)) {
					unlink($oldimg);
				}
				


				$ext=$request->file('img')->getClientOriginalExtension();
    			$filename=time().mt_rand(100000,999999).'.'.$ext;
    			$request->file('img')->move('./uploads',$filename);
    			$data['img']=$filename;

			}
		}


		$res=DB::table('ad')->where('id',$id)->update($data);
    	if($res)
        {
            return redirect('/admin/ad/index')->with(['info'=>'修改成功']);
        }else
        {
            return back()->with(['info'=>'修改失败']);
        }
	}
//编辑修改
//删除
	public function getDelete($id)
	{
		$img=DB::table('ad')->where('id',$id)->first()->img;
		$oldimg='./ads/'.$img;
		$res=DB::table('ad')->delete($id);
		if($res)
        {
        	if (file_exists($oldimg)) {
					unlink($oldimg);
				}
            return redirect('/admin/ad/index')->with(['info'=>'删除成功']);
        }else
        {
            return back()->with(['info'=>'删除失败']);
        }

	}
//删除
    public function postUpdatest(Request $request)
    {
    	$id=$request->id;
    	
    	$data=DB::table('ad')->select('status')->where('id',$id)->first();

    	if($data->status==0)
    	{
    		// return 2222;
    		$res=DB::table('ad')->where('id',$id)->update(['status'=>1]);
    		if($res)
    		{
    			return response()->json(1);
    		}else
    		{
    			return response()->json(5);
    		}
    	}elseif($data->status==1)
    	{
    		// return 1111;
    		$res=DB::table('ad')->where('id',$id)->update(['status'=>0]);
    		if($res)
    		{

    			return response()->json(0);
    		}else
    		{
    			return response()->json(5);
    		}
    		
    	}




    }
}
